package com.china08.yxyapi.api.manage.tec.teaching.scoreport.baseanalysis;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.scoreport.Exam;
import com.china08.yxyapi.entity.mg.scoreport.ExamStudent;
import com.china08.yxyapi.entity.mg.scoreport.report.ExamBaseAnalyse;
import com.china08.yxyapi.entity.mg.scoreport.report.ExamBaseAnalyse.ScoreLevel;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.scoreport.ExamBaseAnalyseRepository;
import com.china08.yxyapi.repository.mg.scoreport.ExamRepository;
import com.china08.yxyapi.repository.mg.scoreport.ExamStudentRepository;

/**
 * 创建考试 基础分析
 * 
 */
@CrossOrigin
@RestController
public class BaseAnalysisController extends YxyApiBaseController {
    @Autowired
    private ExamBaseAnalyseRepository examBaseAnalyseRepository;
    @Autowired
    private ExamRepository examRepository;
    @Autowired
    private ExamStudentRepository examStudentRepository;
    @RequestMapping(value = "/api_yw/manage/tec/teaching/scoreport/baseanalysis/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        RespModel model = new RespModel();
        Exam exam = new Exam();
        exam.setId(reqModel.examId);
        if (examRepository.findById(reqModel.examId) != null) {
        	model.totalScore = examRepository.findById(reqModel.examId).getTotalScore();
		}else{			
			model.totalScore = 0;
		}
        // 查询到基础分析实体类
        ExamBaseAnalyse examBaseAnalyse = examBaseAnalyseRepository.findByExam(exam);
        List<Integer> keyLst = new ArrayList<Integer>();
        for (String key : examBaseAnalyse.getHistogram().keySet()) {
            keyLst.add(Integer.parseInt(key));
        }
        Collections.sort(keyLst);
        List<Integer> histogram = new ArrayList<Integer>();// 值
        List<String> keyLst2 = new ArrayList<String>();// key
        for (int i = 0; i < keyLst.size(); i++) {
            if (i == 0) {
                keyLst2.add("0-" + keyLst.get(i).toString());
                histogram.add(examBaseAnalyse.getHistogram().get(keyLst.get(i).toString()));
            } else if (i == keyLst.size() - 1) {
                keyLst2.add(keyLst.get(i - 1).toString() + "-" + keyLst.get(i).toString());
                histogram.add(examBaseAnalyse.getHistogram().get(keyLst.get(i).toString()));
                break;
            } else {
                keyLst2.add(keyLst.get(i - 1).toString() + "-" + keyLst.get(i).toString());
                histogram.add(examBaseAnalyse.getHistogram().get(keyLst.get(i).toString()));
            }

        }
        
        // 不及格的人占的百分比
        Long bujigeCount = examStudentRepository.countByExamAndStuTotalScoreLessThan(exam, examBaseAnalyse.getFineRank().get(2));
        Long studentCountNotJoin = examStudentRepository.countByExamAndStuTotalScoreNot(exam, -1);
        DecimalFormat df = new DecimalFormat("######0.00");
        Integer bujige = (int) (Double.parseDouble(df.format((double) bujigeCount / studentCountNotJoin)) * 100);
        
        
        model.keyLst = keyLst2;
        model.histogram = histogram;
        model.average = examBaseAnalyse.getAverage();
        model.max = examBaseAnalyse.getMax();
        model.min = examBaseAnalyse.getMin();
        model.scoreRate =  (int)(examBaseAnalyse.getScoreRate()*1);
        model.scoreLevel = examBaseAnalyse.getScoreLevel();
        List<String> scoreLevelName = new ArrayList<>();
        for (Entry<String, ScoreLevel> entry : examBaseAnalyse.getScoreLevel().entrySet()) {
            if (entry.getValue().concentrate.equals("集中度：高")) {
                switch (entry.getKey()) {
                case "high":
                    scoreLevelName.add("高分段");
                    break;
                case "middle":
                    scoreLevelName.add("中分段");
                    break;
                case "low":
                    scoreLevelName.add("低分段");
                    break;
                }
            }
        }
        
        //班级里的每个学生成绩
        List<ExamStudent>  students= examStudentRepository.findByExam(exam);
        List<Double> stuTotalScoreLst=new ArrayList<Double>();
        
        for (ExamStudent examStudent : students) {
        	stuTotalScoreLst.add(examStudent.getStuTotalScore());
		}
        model.scoreLevelName = StringUtils.join(scoreLevelName.toArray(), ",");
        model.fineRank = examBaseAnalyse.getFineRank();
        model.examBaseId = examBaseAnalyse.getId();
        model.personCount = bujige;
        model.stuTotalScoreLst = stuTotalScoreLst;
        return model;
    }

    protected static class RespModel extends YxyApiRespModel {
        // value
        public List<Integer> histogram;
        // key
        public List<String> keyLst;
        // 平均分
        public Double average;
        // 最高分
        public Double max;
        // 最低分
        public Double min;
        // 得分率
        public Integer scoreRate;
        // "high","middle","low"
        public Map<String, ExamBaseAnalyse.ScoreLevel> scoreLevel;
        // 优良分布图 key 5,4,3,2,1 分别对应 "优秀","良好","及格","不及格","不及格过差"</br>
        public Map<Integer, Integer> fineRank;
        // 基础分析实体类的id
        public String examBaseId;
        // 试卷总分
        public Integer totalScore;
        // 分段名称
        public String scoreLevelName;
        //不及格人数
        public Integer personCount;
        //班级里每个学生的成绩
        public List<Double> stuTotalScoreLst;
    }

    protected static class ReqModel extends YxyApiReqModel {
        // 考试id
        public String examId;
    }
}
